//
//  FoodVideoSql.m
//  FoodFamily
//
//  Created by Bang-guo on 15-6-24.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "FoodVideoSql.h"

@implementation FoodVideoSql
+(instancetype)shareFoodViedoSql{
    static FoodVideoSql *foodVideo = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        foodVideo = [[FoodVideoSql alloc] init];
        
    });

    return foodVideo;

}
static sqlite3 *db = nil;
-(void)OpenDB//打开数据库
{
    if (db != nil) {
        return;
    }
    NSArray *array  =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"dataBase.sqlite"];
    NSLog(@"***%@",sqlitePath);
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result ==SQLITE_OK) {
        NSLog(@"打开数据库成功");
    }else
    {
    
        NSLog(@"打开数据库失败");
    }

    
}
-(void)creatTable
{
NSString *sql = @"CREATE TABLE IF NOT EXISTS foodvideo(number INTEGER PRIMARY KEY AUTOINCREMENT,englishName TEXT,name TEXT,imagePathLandscape TEXT)";

//    self.englishNamelable.text = mov.englishName;
//    self.namelable.text = mov.name;
//    NSURL *url =[NSURL URLWithString:mov.imagePathLandscape];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    
    }
}
-(void)insertMovie:(Movie *)mov
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO foodvideo(englishName,name,imagePathLandscape)VALUES('%@','%@','%@')",mov.englishName,mov.name,mov.imagePathLandscape];
    //NSLog(@"//////////%@",sql);
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"添加成功");
    }else
    {
    
        NSLog(@"添加失败");
    }


}
-(NSMutableArray *)selectallMovie
{
NSString *sql = @"SELECT * FROM foodvideo";

    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result ==SQLITE_OK) {
        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            
        
        const unsigned char *englishName = sqlite3_column_text(stmt, 1);
        const unsigned char *name = sqlite3_column_text(stmt, 2);
        const unsigned char *imagePathLandscape = sqlite3_column_text(stmt, 3);
        Movie *movie = [[Movie alloc]init];
        movie.englishName = [NSString stringWithUTF8String:(const char *)englishName];
        movie.name = [NSString stringWithUTF8String:(const char *)name];
        movie.imagePathLandscape = [NSString stringWithUTF8String:(const char *)imagePathLandscape];
        [array addObject:movie];
    }


    return array;
    }else{
        NSLog(@"准备失败");
        return array;
    
    }
}
-(void)dropTable
{
NSString *sql = @"DROP TABLE foodvideo";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
    
        NSLog(@"删除失败");
    
    }


}
@end
